﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     qudpsocketclient.h
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-08 23:42
///////////////////////////////////////////////////////////////////////////
#ifndef QUDPSOCKETCLIENT_H
#define QUDPSOCKETCLIENT_H

#include <QThread>
#include <QUdpSocket>

class QUdpSocketClient : public QThread
{
    Q_OBJECT
public:
    explicit QUdpSocketClient(QString m_localAddress,uint16_t m_localPort,QString m_desAddress,uint16_t m_desPort,QObject *parent = nullptr);
    ~QUdpSocketClient();
    void open();
    void close();
signals:
    void signal_openSocket();
    void signal_closeSocket();
private slots:
    void slot_openSocket();
    void slot_closeSocket();
private:
    QString localAddress;
    uint16_t localPort;
    QString desAddress;
    uint16_t desPort;
    QUdpSocket *udpSocket;

private:
    uint16_t sendPackId;
    uint16_t getSendPackId();
    uint16_t recvPackId;
    uint32_t nextFrameSerial; //下一帧序号
    QByteArray recvFrameTempData;
    QByteArray recvPackData; //临时接收数据
protected:
    void run() override;
public slots:
    void readyRead();
    void slot_sendData(QByteArray byteArray);
private:
    void parsingFrameData(const QByteArray &byteArray); //解析数据帧
signals:
    void signal_recvData(QByteArray byteArray);
};

#endif // QUDPSOCKETCLIENT_H
